import 'dart:async';

import 'package:bot_toast/bot_toast.dart';
import 'package:flutter/material.dart';
import 'package:shopping_group/base/base_constant.dart';
import 'package:shopping_group/baseblocs/bloc_provider.dart';
import 'package:shopping_group/event/event.dart';
import 'package:shopping_group/http/apis/apis.dart';
import 'package:shopping_group/http/http_request_all.dart';
import 'package:shopping_group/modle/more/entity/group_fulipush_entity.dart';
import 'package:shopping_group/modle/more/entity/group_list_simple4fuli_entity.dart';
import 'package:shopping_group/modle/more/entity/group_simple4fuli_entity.dart';
import 'package:shopping_group/modle/more/entity/group_simple_entity.dart';
import 'package:shopping_group/utils/network_utils.dart';
import 'package:shopping_group/utils/object_util.dart';
import 'package:shopping_group/utils/route_util.dart';

class MoreBloc implements BlocBase {
  List<GroupSimpleEntity> _list = [];
  List<GroupSimple4FuliEntity> _fllist = [];
  List<GroupSimple4FuliEntity> _flcjlist = [];
  StreamController<Object> _initController = StreamController<Object>();
  StreamSink get initDate => _initController.sink;

  StreamController<Object> _addController = StreamController<Object>();
  StreamSink get _addDate => _addController.sink;
  Stream<Object> get sendData => _addController.stream;

  StreamController<Object> _initFlController = StreamController<Object>();
  StreamSink get initFlDate => _initFlController.sink;

  StreamController<Object> _flController = StreamController<Object>();
  StreamSink get _addFlDate => _flController.sink;
  Stream<Object> get sendFlData => _flController.stream;

  StreamController<Object> _initFlcjController = StreamController<Object>();
  StreamSink get initFlcjDate => _initFlcjController.sink;

  StreamController<Object> _flcjController = StreamController<Object>();
  StreamSink get _addFlcjDate => _flcjController.sink;
  Stream<Object> get sendFlcjData => _flcjController.stream;

  StreamController<Object> _initvalidateFlController =
      StreamController<Object>();
  StreamSink get initValidateFlDate => _initvalidateFlController.sink;

  MoreBloc() {
    _initController.stream.listen(initData);
    _initFlController.stream.listen(initFlData);
    _initvalidateFlController.stream.listen(initValidData);
    _initFlcjController.stream.listen(initFlcjData);
  }

  void initFlcjData(data) {
    _initFlcjTask(data);
  }

  void initValidData(data) {
    _initValidTask(data);
  }

  void initData(data) {
    _initTask(data);
  }

  void initFlData(data) {
    _initFlTask(data);
  }

  Future _initValidTask(data) async {
    FocusScope.of(RouteUtil.navigatorKey.currentContext)
        .requestFocus(FocusNode());

    NetworkUtils.isNetworkConnected(connectedCallback: (connected) {
      if (!connected) {
        BotToast.showText(text: '网络无法连接');
      } else {
        BotToast.showLoading();
        HttpUtilAll.post(Apis.FL_VALIDATE_URL, data: data, headers: {},
            success: (result) {
          BotToast.closeAllLoading();
          if (null != result) {
            GroupFuliPushEntity params = GroupFuliPushEntity.fromJson(result);
            eventBus.fire(StartFlVideoEvent(data['groupSpecId'], params.pushId,
                params.videoUrl, params.videoFileId, data['entity']));
          }
        }, error: (errorMsg) {
          BotToast.closeAllLoading();
          // 请求失败返回的错误信息
          BotToast.showText(text: errorMsg);
        });
      }
    });
  }

  Future _initFlcjTask(data) async {
    FocusScope.of(RouteUtil.navigatorKey.currentContext)
        .requestFocus(FocusNode());
    Map<String, dynamic> data = {};
    if (ObjectUtil.isEmptyString(BaseConstant.userId)) {
      return;
    }
    data['val'] = BaseConstant.userId;
    NetworkUtils.isNetworkConnected(connectedCallback: (connected) {
      if (!connected) {
        BotToast.showText(text: '网络无法连接');
      } else {
        HttpUtilAll.post(Apis.CJFL_URL, data: data, headers: {},
            success: (result) {
          _flcjlist?.clear();
          BotToast.closeAllLoading();
          if (null != result) {
            GroupListSimple4FuliEntity entity =
                GroupListSimple4FuliEntity.fromJson(result);
            List list = List.from(entity.lstGroup);
            list.forEach((element) {
              if (element is GroupSimple4FuliEntity) {
                _flcjlist.add(element);
              }
            });
            eventBus.fire(HideFlclEven(entity.beDisplay));
            _addFlcjDate.add(_flcjlist);
          }
        }, error: (errorMsg) {
          BotToast.closeAllLoading();
          // 请求失败返回的错误信息
          BotToast.showText(text: errorMsg);
        });
      }
    });
  }

  Future _initFlTask(data) async {
    FocusScope.of(RouteUtil.navigatorKey.currentContext)
        .requestFocus(FocusNode());
    Map<String, dynamic> data = {};
    if (ObjectUtil.isEmptyString(BaseConstant.userId)) {
      return;
    }
    data['val'] = BaseConstant.userId;
    NetworkUtils.isNetworkConnected(connectedCallback: (connected) {
      if (!connected) {
        BotToast.showText(text: '网络无法连接');
      } else {
        HttpUtilAll.post(Apis.FL_LIST_URL, data: data, headers: {},
            success: (result) {
          _fllist?.clear();
          BotToast.closeAllLoading();
          if (null != result) {
            GroupListSimple4FuliEntity entity =
                GroupListSimple4FuliEntity.fromJson(result);
            List list = entity.lstGroup;
            list.forEach((element) {
              if (element is GroupSimple4FuliEntity) {
                _fllist.add(element);
              }
            });
            eventBus.fire(HideFlEven(entity.beDisplay));
            _addFlDate.add(_fllist);
          }
        }, error: (errorMsg) {
          BotToast.closeAllLoading();
          // 请求失败返回的错误信息
          BotToast.showText(text: errorMsg);
        });
      }
    });
  }

  Future _initTask(data) async {
    FocusScope.of(RouteUtil.navigatorKey.currentContext)
        .requestFocus(FocusNode());
    // BotToast.showLoading();
    NetworkUtils.isNetworkConnected(connectedCallback: (connected) {
      if (!connected) {
        BotToast.showText(text: '网络无法连接');
      } else {
        HttpUtilAll.post(Apis.ALL_GROUP_URL, data: data, headers: {},
            success: (result) {
          _list?.clear();
          BotToast.closeAllLoading();
          if (null != result) {
            List list = List.from(result);
            list.forEach((element) {
              GroupSimpleEntity params = GroupSimpleEntity.fromJson(element);
              _list.add(params);
            });
            _addDate.add(_list);
          }
        }, error: (errorMsg) {
          BotToast.closeAllLoading();
          // 请求失败返回的错误信息
          BotToast.showText(text: errorMsg);
        });
      }
    });
  }

  @override
  void dispose() {
    _initController?.close();
    _initFlController?.close();
    _flController?.close();
    _addController?.close();
    _initvalidateFlController?.close();
    _initFlcjController?.close();
    _flcjController?.close();
  }
}
